package com.demo.network.aio.client;

import java.io.IOException;
import java.util.Scanner;

public class Client {
    private static String DEFAULT_HOST = "127.0.0.1";
    private static int DEFAULT_PORT = 9091;
    private static AsyncClientHandler clientHandler;

    public static void start() {
        start(DEFAULT_HOST, DEFAULT_PORT);
    }

    public static synchronized void start(String ip, int port) {
        if (clientHandler != null) {
            return;
        }
        clientHandler = new AsyncClientHandler(ip, port);
        new Thread(clientHandler, "client").start();
    }

    public static boolean sendMsg(String msg) throws IOException {
        if (msg.equals("q")) return false;
        clientHandler.sendMsg(msg);
        return true;
    }

    public static void main(String[] args) throws Exception {
        Client.start();
        System.out.println("请输入请求内容...");
        Scanner scanner = new Scanner(System.in);
        while (Client.sendMsg(scanner.nextLine())) ;
    }

}
